package calorie.com.calorie.adapter;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.ido.ble.BLEManager;
import com.ido.ble.bluetooth.device.BLEDevice;
import com.ido.ble.callback.BindCallBack;
import com.ido.ble.callback.UnbindCallBack;
import com.lxh.baselibray.util.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import calorie.com.calorie.R;
import calorie.com.calorie.activity.ScanDeviceActivity;
import calorie.com.calorie.util.StaticValue;


public class ScanAdapter extends BaseAdapter implements View.OnClickListener {

    private LayoutInflater inflater;
    private List<BLEDevice> entities;
    private BLEDevice selectDevice = null;
    private Context context;
    private ProgressDialog progressDialog;
    private TextView clickTextView;
    private String address;
    private String bleName;

    public ScanAdapter(Context context, List<BLEDevice> refCourse) {
        setEntities(refCourse);
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void setEntities(List<BLEDevice> entities) {
        if (entities != null) {
            this.entities = entities;
        } else {
            this.entities = new ArrayList<BLEDevice>();
        }

    }

    public void upDada(BLEDevice device) {
        this.entities.add(device);
        Collections.sort(entities);
        this.notifyDataSetChanged();
    }

    public void clear() {
        this.selectDevice = null;
        this.entities.clear();
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return entities.size();
    }

    public void setSelectDevice(BLEDevice device) {
        this.selectDevice = device;
        notifyDataSetChanged();
    }

    @Override
    public BLEDevice getItem(int index) {
        return entities.get(index);
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_device, null);
            holder = new ViewHolder();
            holder.tvName = convertView.findViewById(R.id.tv_basic_name);
            holder.tvMac = convertView.findViewById(R.id.tv_basic_mac);
            holder.view = convertView.findViewById(R.id.view_select);
            holder.view.setOnClickListener(this);
            holder.view.setTag(entities.get(position));
            holder.tvRssi = (TextView) convertView.findViewById(R.id.tvRssi);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final BLEDevice course = entities.get(position);
        holder.tvName.setText(course.mDeviceName);
        holder.tvMac.setText(course.mDeviceAddress);
        holder.tvRssi.setText(course.mRssi + "");
        if (selectDevice != null && selectDevice.mDeviceAddress.equals(course.mDeviceAddress)) {
            holder.view.setVisibility(View.VISIBLE);
            String Myaddress = StaticValue.userAssets.getBleAddress();
            if (Myaddress != null && Myaddress.equals(entities.get(position).mDeviceAddress)) {
                holder.view.setText("解绑");

            }
        } else {
            holder.view.setVisibility(View.GONE);
        }
        return convertView;
    }




    class ViewHolder {
        private TextView tvName;
        private TextView tvMac;
        private TextView view;
        TextView tvRssi;

    }


    public BindCallBack.ICallBack bindCallBack = new BindCallBack.ICallBack() {
        @Override
        public void onSuccess() {
            closeProgressDialog();

            //修改用户蓝牙信息到服务器

            String Myaddress = StaticValue.userAssets.getBleAddress();
            if (address != null && address.equals(Myaddress)) {
                Toast.makeText(context, "无须重复绑定", Toast.LENGTH_LONG).show();
            }


        }

        @Override
        public void onFailed(BindCallBack.BindFailedError bindFailedError) {
            ToastUtils.showLongToast(context, "绑定失败");
            closeProgressDialog();

        }

        protected void closeProgressDialog() {
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }

        @Override
        public void onCancel() {
            closeProgressDialog();
            Toast.makeText(context, "bind cancel", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onReject() {
            closeProgressDialog();
            Toast.makeText(context, "bind reject", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNeedAuth() {

        }
    };

    public UnbindCallBack.ICallBack unbindCallBack = new UnbindCallBack.ICallBack() {
        @Override
        public void onSuccess() {


            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
            if(ScanDeviceActivity.num!=-1) {
                clickTextView.setText("绑定");
                bleName = null;
                address = null;

            }

        }

        @Override
        public void onFailed() {

            Toast.makeText(context, "解绑失败", Toast.LENGTH_LONG).show();
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_select:
                clickTextView = (TextView) v;
                if (clickTextView.getText().toString().equals("绑定")) { //绑定
                    BLEManager.bind();
                    showProgressDialog("绑定中...");
                    BLEDevice device = (BLEDevice) v.getTag();
                    address = device.mDeviceAddress;
                    bleName = device.mDeviceName;
                } else { //解绑
                    BLEManager.unbind();
                    showProgressDialog("解绑中...");
                }

                break;
        }

    }

    public void showProgressDialog(String title) {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        progressDialog = new ProgressDialog(context);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setTitle(title);
        progressDialog.show();
    }





}
